<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="title" v-model="visible" align-center width="500px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="驳回原因">
        <div class="grid grid-cols-2 gap-x-3 w-full">
          <VSelect v-model="form.reject_type" placeholder="请选择" :options="options.reject_type" class="w-full"
            @change="form.reject_value = form.reject_type === 23 ? 1 : ''" :clearable="false" />
          <el-input-number v-if="form.reject_type === 23" v-model="form.reject_value" controls-position="right" :min="1"
            :step="1" :precision="0" placeholder="请输入" class="w-full" />
          <el-date-picker v-if="form.reject_type === 25" v-model="form.reject_value" placeholder="日期选择" type="date"
            value-format="YYYY-MM-DD" class="w-full" />
        </div>
      </el-form-item>
      <el-form-item label="驳回备注">
        <el-input v-model="form.reject_reason" placeholder="驳回备注" type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const title = ref('驳回')

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (pdcs_ids) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  if (pdcs_ids.length > 1) {
    title.value = '批量驳回'
  }
  form.value = {
    reject_type: null,
    pdcs_ids
  }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  if (!await formRef.validate()) return
  loadings.create = true
  api.reject(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>